#define DBC_CHECK_PRECONDITION

using System;
using System.Xml;

using DesignByContract;

using Outcoding.GraphGenerator.Entities;
using Outcoding.GraphGenerator.Util.Logging;

namespace Outcoding.GraphGenerator.XmlBuilders {
    public class PresentationXmlBuilder : IXmlBuilder {
        private Presentation mResultObject = null;

        public object ResultObject {
            get {
                return mResultObject;
            }
        }

        public PresentationXmlBuilder(XmlNode presentationXmlNode) {
            Check.Require(presentationXmlNode != null, "Xml source node cannot be null");
            Check.Require(presentationXmlNode.Name.CompareTo(XmlTagNames.presentation.ToString()) == 0, "A Presentation Xml node must be supplied.");
            this.BuildObject(presentationXmlNode);
        }

        private void BuildObject(XmlNode presentationXmlNode) {
            mResultObject = new Presentation();
            try {
                if (presentationXmlNode.Attributes["template-url"] != null)
                    mResultObject.TemplatePath = ParseTemplateUrlAttribute(presentationXmlNode);
            } catch (DesignByContract.PreconditionException preExc) {
                //log error
                mResultObject = null;
                LogManager.Instance.LogError("Presentation object could not be created.", preExc);
            }
        }

        private string ParseTemplateUrlAttribute(XmlNode presentationXmlNode) {
            Check.Require(presentationXmlNode.Attributes["template-url"] != null, "template-url attribute not present");
            return presentationXmlNode.Attributes["template-url"].Value;
        }
    }
}
